﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.IO;

namespace MedicalInterface.ViewModel
{
    public class AddUserViewModel : BaseViewModel
    {
        #region variables
        private bool _closeSignal;
        private UserServiceReference.ServiceUserClient serviceUserClient;
        private ObservableCollection<UserServiceReference.User> _listUser;
        private string _login;
        private string _pwd;
        private string _name;
        private string _firstname2;
        private string _role;
        private Byte[] _picture;
        #endregion

        #region commandes
        private ICommand _cmd_ok;
        private ICommand _cmd_browse;
        #endregion

        #region getter / setter
        /// <summary>
        /// command pour OK
        /// </summary>
        public ICommand cmd_ok
        {
            get
            {
                if (_cmd_ok == null)
                {
                    _cmd_ok = new RelayCommand(Confirm, CanOk);
                }
                return _cmd_ok;
            }
            set { _cmd_ok = value; }
        }
        /// <summary>
        /// command pour Browse
        /// </summary>
        public ICommand cmd_browse
        {
            get
            {
                if (_cmd_browse == null)
                {
                    _cmd_browse = new RelayCommand(Browse, CanOk);
                }
                return _cmd_browse;
            }
            set { _cmd_browse = value; }
        }
        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }


        /// <summary>
        /// MDP de la personne
        /// </summary>
        public string Pwd
        {
            get { return _pwd; }
            set
            {
                if (_pwd != value)
                {
                    _pwd = value;
                    OnPropertyChanged("Pwd");
                }
            }
        }

        /// <summary>
        /// login de la personne
        /// </summary>
        public string Login
        {
            get { return _login; }
            set
            {
                if (_login != value)
                {
                    _login = value;
                    OnPropertyChanged("Login");
                }

            }
        }


        /// <summary>
        /// Role de la personne
        /// </summary>
        public string Role
        {
            get { return _role; }
            set
            {
                if (_role != value)
                {
                    _role = value;
                    OnPropertyChanged("Role");
                }

            }
        }
        /// <summary>
        /// Nom de la personne
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }

            }
        }

        /// <summary>
        /// Prénom de la personne
        /// </summary>
        public string FirstName2
        {
            get { return _firstname2; }
            set
            {
                if (_firstname2 != value)
                {
                    _firstname2 = value;
                    OnPropertyChanged("FirstName2");
                }

            }
        }        
        
        #endregion

        /// <summary>
        /// constructeur
        /// </summary>
        public AddUserViewModel(ref ObservableCollection<UserServiceReference.User> listUser)
        {
            //init variables
            serviceUserClient = new UserServiceReference.ServiceUserClient();
            _listUser = listUser;
            //commandes
        }

        private void Browse(object parameter)
        {
            Byte[] FileInbytes;
            
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Images Sources (*.jpg, *.bmp, *.png)|*.jpg*;*.bmp*;*.png|All Files|*.*";
            dlg.Multiselect = false;

            if (dlg.ShowDialog() ?? false)
            {
                //open the file as a stream
                using (Stream s = dlg.OpenFile())
                {

                    // read s into a byte buffer.
                    _picture = new Byte[s.Length + 1];
                    FileInbytes = new Byte[s.Length];
                    int numBytesToRead = (int)s.Length;
                    int numBytesRead = 0;

                    while (numBytesToRead > 0)
                    {

                        // Read may return anything from 0 to numBytesToRead.
                        int n = s.Read(FileInbytes, numBytesRead, numBytesToRead);

                        // The end of the file is reached.
                        if (n == 0) break;

                        numBytesRead += n;
                        numBytesToRead -= n;

                    }
                    s.Close();

                    string sUploadedFileName = dlg.SafeFileName;
                    _picture = FileInbytes;

                    // Configure the message box to be displayed
                    string messageBoxText = "L'Image a bien été chargée ('"+sUploadedFileName+"' | " + numBytesRead + " octets)";
                    string caption = "Confirmation de l'Upload";
                    MessageBoxButton button = MessageBoxButton.OK;
                    MessageBoxImage icon = MessageBoxImage.Information;
                    // Display message box
                    MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);

                    // Process message box results
                    if (result == MessageBoxResult.OK)
                    {

                    }
                } 
            }
        }
              
        /// <summary>
        /// Le bouton OK est-il accessible?
        /// </summary>
        /// <param name="parameter">Type Object</param>
        /// <returns>True</returns>
        private bool CanOk(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Valide la création d'un Utilisateur
        /// </summary>
        /// <param name="parameter"></param>
        private void Confirm(object parameter)
        {
            bool result = false;
            UserServiceReference.User user = new UserServiceReference.User();
            
            
            user.Login = _login;
            user.Name = _name;
            user.Firstname = _firstname2;
            user.Role = _role;
            user.Pwd = _pwd;
            user.Picture = _picture;
            user.Connected = false;

            try
            {
                result = serviceUserClient.AddUser(user);
            }
            catch { }
            _listUser.Add(user);
            CloseSignal = true;
        }        
    }
}
